package edu.ustb.efws.base.service;

import edu.ustb.efws.BaseException;
import edu.ustb.efws.base.domain.Reason;
import edu.ustb.efws.base.mapper.ReasonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 风险原因业务服务
 *
 * @author TS Group
 */
@Service
@Transactional(readOnly = true)
public class ReasonService {
    private final ReasonMapper mapper;

    @Autowired
    public ReasonService(ReasonMapper mapper) {
        this.mapper = mapper;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Reason save(Reason t){
        t.setCreateTime(new Date());

        if(mapper.insert(t) == 0){
            throw new BaseException("新增风险原因失败");
        }

        return t;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public Reason update(Reason t){
        if(mapper.update(t) == 0){
            throw new BaseException("修改风险原因失败");
        }
        return mapper.findOne(t.getId());
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public boolean delete(int id){
        return mapper.delete(id) > 0;
    }

    public Reason get(int id){
        Reason t = mapper.findOne(id);
        if(t == null){
            throw new BaseException("风险原因不存在");
        }
        return t;
    }

    public List<Reason> queryRisk(int riskType, float score){
        return mapper.findRisk(riskType, score);
    }

    public Long count(Integer type, Integer caseId, String caseInfo){
        return mapper.count(type, caseId, caseInfo);
    }

    public List<Reason> query(Integer type, Integer caseId, String caseInfo, Integer offset, Integer limit){
        return mapper.find(type, caseId, caseInfo, offset, limit);
    }
}
